package com.zxh.util;

import com.zxh.audio.AudioRecorder;
import com.zxh.audio.MyRecord;
import com.zxh.audio.PlayUtil;
import org.apache.log4j.Logger;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * Created by zhangxh on 2016/7/11.
 */
public class RecordUtil {
    private static Logger logger=Logger.getLogger(RecordUtil.class);
    public static File record(long time, AudioFileFormat.Type type, boolean play){
        try {
            final AudioRecorder audioRecorder=new AudioRecorder();
            AudioFormat format = getFormat16();
            audioRecorder.setAudioFormat(format);
            logger.info("开始录音");
            audioRecorder.startRecording();
            Thread.sleep(time);
            audioRecorder.stopRecording();
            logger.info("结束录音");
            Thread.sleep(1000);//停止录音后留1秒处理时间，程序把数据写进流里面
            String tempFilePath="sound" + File.separator + "tmp"+File.separator+System.currentTimeMillis()+".wav";
            File file=new File(tempFilePath);
            audioRecorder.saveToFile(file, type);
            if(play){
                new Thread(new Runnable() {
                    public void run() {
                        logger.info("播放录音");
                        audioRecorder.getAudioPlayer().play();
                        logger.info("结束播放");
                    }
                }).start();

//                ThreadPool.execute(new Runnable() {
//                    public void run() {
//                        logger.info("播放录音");
//                        audioRecorder.getAudioPlayer().play();
//                        logger.info("结束播放");
//                    }
//                });
//                ThreadPool.shutdown();
            }
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static AudioFormat getFormat8(){
        AudioFormat format = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            8000f, //44.1 kHz sampling rate = CD quality audio
            16, //1 bytes, 8 bit audio (1 * 8 bit byte)
            1, //1 channels, stereo
            2, //frame size = number of bytes in a sample * number of channels = Default WAVE
            8000, //frame rate = sampling rate = Default WAVE
            false //little endian = Default WAVE
        );
        return format;
    }
    public static AudioFormat getFormat16(){
        AudioFormat format = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                16000f, //44.1 kHz sampling rate = CD quality audio
                16, //1 bytes, 8 bit audio (1 * 8 bit byte)
                1, //1 channels, stereo
                2, //frame size = number of bytes in a sample * number of channels = Default WAVE
                16000, //frame rate = sampling rate = Default WAVE
                false //little endian = Default WAVE
        );
        return format;
    }

    /**
     * 使用自己的录音器
     * @param time
     * @param type
     * @param play
     * @return
     */
    public static File record2(long time, AudioFileFormat.Type type, boolean play){
        try {
            MyRecord.open();
            logger.info("开始录音");
            MyRecord.start();
            Thread.sleep(time);
            MyRecord.stop();
            logger.info("结束录音");
            Thread.sleep(1000);
            String tempFilePath="sound" + File.separator + "tmp"+File.separator+System.currentTimeMillis()+".wav";
            final File file=new File(tempFilePath);
            MyRecord.saveToFile(file,type);
            if(play){
                new Thread(new Runnable() {
                    public void run() {
                        logger.info("播放录音");
                        try {
                            PlayUtil.play(file);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        logger.info("结束播放");
                    }
                }).start();
            }
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
